package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.bean.SystemContextHolder;
import com.base.vistter.iframe.entity.SysUser;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.SysUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/sys/user")
public class SysUserController {

    @Resource(name = "sysUserService")
    private SysUserService sysUserService;

    @GetMapping
    public Result<Page<SysUser>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum, String username, String workNo, String phone, Integer sysDepartId) throws IframeException {
        Page<SysUser> page = Page.of(pageNo, pageNum);
        return Result.ok(sysUserService.findPager(page, username, workNo, phone, sysDepartId));
    }

    @GetMapping("/{id}")
    public Result<SysUser> get(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysUserService.getById(id));
    }

    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody SysUser sysUser) throws IframeException {
        return Result.ok(sysUserService.save(sysUser));
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody SysUser sysUser) throws IframeException {
        return Result.ok(sysUserService.update(sysUser));
    }

    @DeleteMapping(produces = "application/json;charset=utf-8")
    public Result<Integer> delete(@RequestBody List<String> ids) throws IframeException {
        return Result.ok(sysUserService.delete(ids));
    }

    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysUserService.delete(id));
    }

    @GetMapping("/status")
    public Result<Boolean> status(@RequestParam("id") Integer id, @RequestParam("status") Integer status) throws IframeException {
        return Result.ok(sysUserService.updateStatus(id, status));
    }

    @GetMapping("/pass")
    public Result<Boolean> pass(@RequestParam("id") Integer id, @RequestParam("password") String password) throws IframeException {
        return Result.ok(sysUserService.pass(id, password));
    }

    @GetMapping("/repass")
    public Result<Boolean> repass(@RequestParam("password") String password) throws IframeException {
        return Result.ok(sysUserService.repass(password));
    }

    @GetMapping("/valid/user")
    public Result<Page<SysUser>> getPageUser(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum, String username, String phone) throws IframeException {
        Page<SysUser> page = Page.of(pageNo, pageNum);
        return Result.ok(sysUserService.getPageUser(page, username, phone));
    }

    @GetMapping("/info")
    public Result<SysUser> get() throws IframeException {
        SysUser sysUser = sysUserService.getCurrentUser();
        return Result.ok(sysUser);
    }

    @PutMapping(path =  "/edit", produces = "application/json;charset=utf-8")
    public Result<Boolean> updateCurrentUser(@RequestBody SysUser sysUser) throws IframeException {
        return Result.ok(sysUserService.updateCurrentUser(sysUser));
    }

    @GetMapping("/avatar")
    public Result<Boolean> avatar(String avatar) throws IframeException {
        SysUser sysUser = new SysUser();
        sysUser.setId(SystemContextHolder.getSessionContext().getUserId());
        sysUser.setAvatar(avatar);
        return Result.ok(sysUserService.updateById(sysUser));
    }

}
